# -*- coding: utf-8 -*-
"""
Created on Thu Sep 30 15:41:04 2021

@author: Administrator
"""

class ListNode(object):  #链表数据类型
    def __init__(self,val,next = None):
        self.val = val
        self.next = next;
        
class Solution:
    def deleteDuplicates(self,head):
        delflag = 1
        flag = 1
        p = head
        while(p!=None and p.next != None): #核心重点！！
            if p.val != p.next.val:
                flag = 1
                p = p.next
            elif flag<delflag:
                flag += 1;
                p = p.next
            else:
                p.next = p.next.next
        return head
    
if __name__ == "__main__":
    node1 = ListNode(1)
    node2 = ListNode(1)
    node3 = ListNode(2)
    node1.next = node2
    node2.next = node3
    
    list1 = []
    solution = Solution()
    print("初始链表:",[node1.val,node2.val,node3.val])
    newlist = solution.deleteDuplicates(node1)
    while(newlist):
        list1.append(newlist.val)
        newlist = newlist.next
    print("删除重复元素后的链表：",list1)